/**
 * 旅游项目JS
 * @author changxp
 */

$(function(){
	 $('#uploadPictureLink').on('click',function(){
	        $('#pictureFile').click();
	    });
	    $('#pictureImg').on('click',function(){
	        $('#pictureFile').click();
	    });
});

// 上传缩略图片
function uploadPicture(){
    $.ajaxFileUpload({
        url:shiro.baseUrl + "uploadPicture",
        secureuri:false,
        fileElementId:"pictureFile",
        dataType: 'json',
        success: function (data) {
            $("#pictureImg").attr("src", data.preUrl + "?v=" + new Date());
            $('#picture').val(data.picture);
        },error:function(XMLHttpRequest, textStatus, errorThrown){
            layer.msg(errorThrown, {icon: 5,time: 1000});
        }
    });
}

//添加旅游项目
function add() {
    var name = $.trim($('#name').val());
    if(shiro.util.isEmpty(name)) {
        layer.tips('项目名称不能为空', '#name');
        return false;
    }
	
    var price = $.trim($('#price').val());
    if(shiro.util.isEmpty(price)) {
        layer.tips('项目价格不能为空', '#price');
        return false;
    }
    var desc = CKEDITOR.instances.desc.getData();
    if(shiro.util.isEmpty(desc)) {
        layer.msg('项目介绍不能为空', {icon: 5,time: 1000});
        return false;
    }
    
    var picture = $('#descPhotos').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('缩略图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    //var status = $('#status').val();
    var status = 2;
    var goods = $("#goodsId option:selected");
    var goodsId = goods.val();
    var goodsName = goods.text();
    // 提交信息
    shiro.ajax({
        url: 'projectInfo/addProject',
        data:{ "goodsId":goodsId,"goodsName":goodsName, "name": name, 'desc': desc, "price": price, "picture": picture, "status": status},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "projectInfo/list";
                    /*if(status == 1){
                        window.location.href = shiro.baseUrl + "projectInfo/merchantList";
                    }else{
                    }*/
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#name').val('');
            }
        }
    });
    return false;
}

// 执行修改项目信息
function editProjectInfo() {
	var goodsId = $.trim($('#goodsId').val());
	/*if(shiro.util.isEmpty(goodsId)) {
        layer.tips('商家名称不能为空', '#goodsId');
        return false;
    }*/
	var name = $.trim($('#name').val());
	if(shiro.util.isEmpty(name)) {
        layer.tips('项目名称不能为空', '#name');
        return false;
    }
    var price = $.trim($('#price').val());
    if(shiro.util.isEmpty(price)) {
        layer.tips('项目价格不能为空', '#price');
        return false;
    }
    var desc = CKEDITOR.instances.desc.getData();
    if(shiro.util.isEmpty(desc)) {
        layer.msg('项目介绍不能为空', {icon: 5,time: 1000});
        return false;
    }
    var picture = $('#picture').val();
    if(shiro.util.isEmpty(picture)) {
        layer.msg('项目图片不能为空', {icon: 5,time: 1000});
        return false;
    }
    var status = $('#status').val();
    // 提交信息
    shiro.ajax({
        url: 'projectInfo/editProject',
        data:{'projectId':$("#projectId").val(),'name':name,'goodsId':goodsId,'desc':desc,'price':price,'picture':picture},
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    if(status == 2){
                        window.location.href = shiro.baseUrl + "projectInfo/list";
                    }else {
                        window.location.href = shiro.baseUrl + "projectInfo/check";
                    }
                   /* if($("#goodsId").length > 0) {
                    }else{
                        window.location.href = shiro.baseUrl + "projectInfo/merchantList";
                    }*/
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#name').val('');
            }
        }
    });
    return false;
}

// 删除旅游项目
function deleteProject(projectId,status) {
    layer.msg('确定删除该旅游项目？', {
        time: 0,
        btn: ['确定', '取消'],
        yes: function(index){
            layer.close(index);
            // 提交删除
            shiro.ajax({
                url: 'projectInfo/deleteProject',
                data:{"projectId": projectId},
                success: function(data) {
                    if(data.result == 0) {
                        layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            if(status == 2){
                                window.location.href = shiro.baseUrl + "projectInfo/list";
                            }else{
                                window.location.href = shiro.baseUrl + "projectInfo/check";
                            }

                        });
                    } else {
                        layer.msg(data.message, {icon: 5,time: 1000});
                    }
                }
            });
        }
    });
}

// 批量删除旅游项目
function batchDeleteProjects() {
    var projectIdArray = shiro.form.getCheckBoxSelectValues('projectInfoIdCheckBox');
    if(projectIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的旅游项目？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var projectIds= projectIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'projectInfo/batchDelete',
                    data:{"projectIds": projectIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                            	window.location.href = shiro.baseUrl + "projectInfo/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}

function adopt(projectId,status,name){
//询问框
    layer.confirm('确定'+name+'？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        shiro.ajax({
            url: 'projectInfo/editStatus',
            data:{'projectId':projectId,'status':status},
            success: function(data) {
                if(data.result == 0) {
                    layer.msg(data.message, {icon: 1,time: 1000}, function() {
                        window.location.href = shiro.baseUrl + "projectInfo/check";
                    });
                } else {
                    layer.msg(data.message, {icon: 5,time: 1000});
                    $('#name').val('');
                }
            }
        });
    }, function(){

    });
}

// 驳回
function reject(projectId) {
    //prompt层
    layer.prompt({title: '请说明驳回理由', formType: 2}, function (text, index) {
        layer.close(index);
        // 确认驳回
        shiro.ajax({
            url: 'projectInfo/editStatus',
                data:{'projectId':projectId,'status':3,'projectDesc':text},
            success: function (data) {
                if(data.result == 0) {
                    layer.msg(data.message, {icon: 1,time: 1000}, function() {
                        window.location.href = shiro.baseUrl + "projectInfo/check";
                    });
                } else {
                    layer.msg(data.message, {icon: 5,time: 1000});
                    $('#name').val('');
                }
            }
        });
    });
}


/**
 * 查看
 */
function viewRestaurant(projectDesc){
    layer.alert(projectDesc, {
        title:"查看驳回原因"
    })
}
